package xuegao.study.mall.oauth2.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import xuegao.study.mall.common.model.oauth.OauthClientDetails;
import xuegao.study.mall.oauth2.service.AppService;
import xuegao.study.mall.repo.dao.oauth.OauthClientDetailsDao;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AppServiceImpl implements AppService {

    private Logger logger = LoggerFactory.getLogger(AppServiceImpl.class);

    @Autowired
    private OauthClientDetailsDao oauthClientDetailsDao;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public int create(OauthClientDetails details) {
        details.setClientSecret(passwordEncoder.encode("123456"));
        details.setScope("all");
        details.setAuthorizedGrantTypes("authorization_code,refresh_token,password");
        details.setAccessTokenValidity(7200);
        details.setRefreshTokenValidity(7200);
        return oauthClientDetailsDao.insert(details);
    }

    @Override
    public int update(OauthClientDetails details) {
        return oauthClientDetailsDao.updateByClientId(details);
    }

    @Override
    public List<OauthClientDetails> listApp() {
        return oauthClientDetailsDao.selectByMap(new HashMap<>());
    }

    @Override
    public int delete(String clientId) {
        Map<String, Object> params = new HashMap<>();
        params.put("client_id", clientId);
        return oauthClientDetailsDao.deleteByMap(params);
    }
}
